<template>
  <div>
    <div class="search" style="display:flex;">
      <el-input placeholder="输入医生名称筛选" style="width: 200px" v-model="name"></el-input>
      <el-input placeholder="输入医生性别筛选" style="width: 200px;margin-left: 10px" v-model="sex"></el-input>
      <el-input placeholder="输入所属医院筛选" style="width: 200px;margin-left: 10px" v-model="hospitalName"></el-input>
      <el-button type="info"  style="margin-left: 20px" @click="load(1)">查询</el-button>
      <el-button type="warning" style="margin-left: 20px" @click="reset">重置</el-button>
      <div style="margin-left: auto;margin-right: 30px">
        <el-button size="medium" type="success" @click="handleAdd" style="margin-left: 20px">新增医生信息</el-button>
      </div>
    </div>

<!--    <div class="operation">
      <el-button type="primary" plain @click="handleAdd">新增</el-button>
    </div>-->

    <div class="table">
      <el-table :data="tableData" strip @selection-change="handleSelectionChange" style="width: 100%;">
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column prop="id" label="序号" align="center" min-width="50">
          <template v-slot="scope">{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column prop="username" label="用户名" align="center" min-width="100"></el-table-column>
        <el-table-column prop="name" label="名称" align="center" min-width="100"></el-table-column>
        <el-table-column prop="sex" label="性别" align="center" min-width="100"></el-table-column>
        <el-table-column prop="avatar" label="头像" align="center" min-width="100">
          <template v-slot="scope">
            <el-image v-if="scope.row.avatar" style="width: 60px;height: 60px" :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="hospitalName" label="所属医院" align="center" min-width="100"></el-table-column>
        <el-table-column prop="officeName" label="所属科室" align="center" min-width="100"></el-table-column>
        <el-table-column label="状态" align="center" min-width="100">
          <template v-slot="scope">
            <el-switch
                @change="handleStatusChange(scope.row.id, scope.row.status)"
                :active-value="'正常'"
                :inactive-value="'禁用'"
                v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="role" label="角色标识" align="center" min-width="120">
          <template v-slot="scope">
            <div class="tag-group">
              <el-tag :key="scope.row.id" :type="''" effect="dark">
                {{ scope.row.role }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="350">
          <template v-slot="scope">
            <el-button size="medium" type="info" @click="lookDetails(scope.row)">查看医生详细信息</el-button>
            <el-button size="medium" type="primary"  @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="medium" type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex;margin-top: 20px">
        <el-select
            size="small"
            v-model="operateType" placeholder="批量操作">
          <el-option v-for="item in operates" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
        <el-button
            style="margin-left: 20px"
            class="search-button"
            @click="handleBatchOperate()"
            type="primary"
            size="small">
          确定
        </el-button>
        <div style="margin-left: auto;">
          <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handlePageChange"
              layout="total, sizes, prev, pager, next, jumper"
              :page-size="pageSize"
              :page-sizes="[5, 10, 15]"
              :current-page.sync="pageNum"
              :total="total">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog title="医生表" :visible.sync="fromVisible" width="50%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
        <el-form-item label="科室图片" prop="avatar">
          <single-upload v-model="form.avatar"></single-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="用户名" ></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex" placeholder="性别">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属城市" prop="city">
          <el-input v-model="form.city" placeholder="所属城市"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="地址"></el-input>
        </el-form-item>
        <el-form-item label="所属科室" prop="officeId">
          <el-select v-model="form.officeId" placeholder="请选择科室" style="width: 100%">
            <el-option
                v-for="item in officeData"
                :key="item.id"
                :value="item.id"
                :label="item.officeName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医生擅长" prop="skill">
          <el-input  type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" v-model="form.skill" placeholder="医生擅长"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input  type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" v-model="form.introduction" placeholder="简介"></el-input>
        </el-form-item>
        <el-form-item label="挂号费" prop="price">
          <el-input v-model="form.price" placeholder="挂号费"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="医生详细信息" :visible.sync="fromVisible2" width="50%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
        <el-form-item label="用户名" prop="username" style="font-weight: bold;">
          <el-input v-model="form.username" placeholder="用户名" disabled="true" style="font-size: 18px;"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name" style="font-weight: bold;">
          <el-input v-model="form.name" placeholder="名称" disabled="true" style="font-size: 18px"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex" style="font-weight: bold;">
          <el-input v-model="form.sex" placeholder="性别" disabled="true" style="font-size: 18px"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address" style="font-weight: bold;">
          <el-input v-model="form.address" placeholder="地址" disabled="true" style="font-size: 18px"></el-input>
        </el-form-item>
        <el-form-item label="挂号费" prop="price" style="font-weight: bold;">
          <el-input v-model="form.price" placeholder="挂号费" disabled="true" style="font-size: 18px"></el-input>
        </el-form-item>
        <el-form-item label="医生擅长" prop="skill" style="font-weight: bold;">
          <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" v-model="form.skill"
                    placeholder="医生擅长" disabled="true" style="font-size: 18px"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="introduction" style="font-weight: bold;">
          <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" v-model="form.introduction"
                    placeholder="简介" disabled="true" style="font-size: 18px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" type="primary" @click="fromVisible2 = false">返回</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import SingleUpload from "@/components/singleUpload.vue";

export default {
  name: "Doctor",
  components: {SingleUpload},
  data() {
    return {
      operates: [
        {label: "批量启用", value: "open"},
        {label: "批量禁用", value: "ban"},
        {label: "批量删除", value: "delete"},
      ],
      operateType: null,
      tableData: [],  // 所有的数据
      officeData:[],
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      name: null,
      sex:null,
      hospitalName:null,
      fromVisible: false,
      fromVisible2: false,
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
      },
      ids: []
    }
  },
  created() {
    this.load(1)
    this.loadOffice()
  },
  methods: {
    loadOffice(){
      this.$request.get('/office/selectAll').then(res => {
        if (res.code === '200') {
          this.officeData = res.data;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    handleBatchOperate() {
      if(this.operateType==null){
        this.$message({
          message: '请选择操作类型',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      if(!this.ids.length){
        this.$message({
          message: '请选择要操作的用户',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      this.$confirm('是否要进行该批量操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids=this.ids;
        switch (this.operateType) {
          case this.operates[0].value:
            this.updateBatch('正常',ids);
            break;
          case this.operates[1].value:
            this.updateBatch('禁用',ids)
            break;
          case this.operates[2].value:
            this.delBatch(ids)
            break;
        }
      }).catch(() => {
        // 取消按钮被点击时的操作
        console.log('用户取消了操作');
      });
    },
    updateBatch(status,ids){
      this.$request.put('doctor/updateBatch', {status:status,ids:ids}).then(response => {
        if (response.code === '200') {  // 表示成功保存
          this.load(1);
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1000
          });
        } else {
          this.$message.error(response.msg);  // 弹出错误的信息
        }
      });
    },
    handleStatusChange(id, status) {
      this.$request.put('doctor/update', {id:id,status:status}).then(response => {
        if (response.code === '200') {  // 表示成功保存
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        } else {
          this.$message.error(response.msg);  // 弹出错误的信息
        }
      });
    },
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
    },
    lookDetails(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible2 = true   // 打开弹窗
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/doctor/update' : '/doctor/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/doctor/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
    delBatch(ids) {   // 批量删除
      if (!ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('您确定批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/doctor/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {
      if (pageNum) this.pageNum = pageNum;
      this.$request.get('/doctor/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          sex:this.sex,
          hospitalName:this.hospitalName
        }
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data?.list || [];
          this.total = res.data?.total || 0;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    reset() {
      this.name = null;
      this.sex=null;
      this.hospitalName=null;
      this.load(1);
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.load(1); // Reset to the first page when changing page size
    },
    handlePageChange(newPage) {
      this.load(newPage);
    }
  }
}
</script>

<style scoped>


</style>
